<?php
namespace app\home\controller;

use app\home\common\Common;
use app\home\common\Page;
use app\home\common\Pagetwo;
use app\infrastructure\uri\Request;
use app\infrastructure\uri\Uri;

/**
 * Created by PhpStorm.
 * User: YD
 * Date: 2017/4/26
 * Time: 16:52
 */
class Userloan extends Common
{
    private $client;
    public  function  __construct(){
        parent::__construct();
        $this->client=new Request();
    }
    public function userloanList()
    {
        $get=$_GET;
        $query=[];
        if(session('role') == 'admin'){
            //线上
            $query['userPhone']=[
                '$or'=>[
                    ['$like'=>'9%'],
                    ['$like'=>'8%'],
                    ['$like'=>'7%'],
                    ['$like'=>'6%'],
                    ['$like'=>'5%'],
                    ['$like'=>'4%'],
                    ['$like'=>'3%'],
                    ['$like'=>'2%']
                ]
            ];
        }elseif(session('role') == 'testing'){
            //测试
            $query['userPhone']=[
                '$like'=>'1111111%'
            ];
        }
        if(!empty($get['userPhone'])){
            $query['userPhone']=$get['userPhone'];
        }
        if(isset($get['auditProcessStatus']) && $get['auditProcessStatus']!=''){
            if($get['auditProcessStatus'] == 11){
                $query['auditProcessStatus']=[0,1,2];
            }else{
                $query['auditProcessStatus']=$get['auditProcessStatus'];
            }
        }
        if(!empty($get['transactionStatus'])){
            if($get['transactionStatus'] == 11){
                $query['transactionStatus']=[0,1,2,3];
            }else{
                $query['transactionStatus']=$get['transactionStatus'];
            }
        }
        if(!empty($get['loanRepayStatus'])){
            if($get['loanRepayStatus'] == 11){
                $query['loanRepayStatus']=[0,1,2,3];
            }else{
                $query['loanRepayStatus']=$get['loanRepayStatus'];
            }
        }

        if(!empty($get['uploanRepayPeriod'])){
            $query['loanRepayPeriod']=[
                '$gte'=>$get['uploanRepayPeriod']
            ];
        }
        if(!empty($get['downloanRepayPeriod'])){
            $query['loanRepayPeriod']=[
                '$lte'=>$get['downloanRepayPeriod']
            ];
        }
        if(!empty($get['uploanRepayPeriod']) && !empty($get['downloanRepayPeriod'])){
            $query['loanRepayPeriod']=[
                '$gte'=>$get['uploanRepayPeriod'],
                '$lte'=>$get['downloanRepayPeriod']
            ];
        }

        if(!empty($get['upcreateTime'])){
            $query['createdAt']=[
                '$gte'=>$get['upcreateTime']
            ];
        }
        if(!empty($get['downcreateTime'])){
            $query['createdAt']=[
                '$lte'=>$get['downcreateTime']
            ];
        }
        if(!empty($get['upcreateTime']) && !empty($get['downcreateTime'])){
            $query['createdAt']=[
                '$gte'=>$get['upcreateTime'],
                '$lte'=>$get['downcreateTime']
            ];
        }

        if(!empty($get['uploanAmount'])){
            $query['loanAmount']=[
                '$gte'=>$get['uploanAmount']
            ];
        }
        if(!empty($get['downloanAmount'])){
            $query['loanAmount']=[
                '$lte'=>$get['downloanAmount']
            ];
        }
        if(!empty($get['uploanAmount']) && !empty($get['downloanAmount'])){
            $query['loanAmount']=[
                '$gte'=>$get['uploanAmount'],
                '$lte'=>$get['downloanAmount']
            ];
        }

        $query['limitation']=3000000;
        $countUrl=Uri::HOST.Uri::POSTSELECTUSERLOAN;
        $totalCount = $this->client->getArrayData($countUrl,$query,3);
        if(isset($_GET['extend']) && $_GET['extend'] == 1){
            $column_name=[
                [
                    'ID',
                    'userID',
                    'userPhone',
                    'transactionID',
                    'transactionStatus',
                    'productID',
                    'loanAmount',
                    'loanRepayPeriod',
                    'managementFee',
                    'loanInterestRate',
                    'actualAmount',
                    'couponID',
                    'userStatus',
                    'auditProcessID',
                    'auditProcessStatus',
                    'loanSubmitTime',
                    'loanArrangedTime',
                    'loanRepayStatus',
                    'createdAt',
                    'updatedAt',
                    'auditFee',
                    'loanInterestRate',
                    'totalRepay',
                    'overdueRate',
                    'sort'
                ]
            ];
            foreach($totalCount['data'] as $k=>$v){
                $totalCount['data'][$k]['createdAt']=$this->gmtToIndiaTime($v['createdAt']);
                $totalCount['data'][$k]['updatedAt']=$this->gmtToIndiaTime($v['updatedAt']);
            }
            $data = array_merge($column_name, $totalCount['data']);
            $this->extendToExcel($data);
        }
        $p=empty(input('p'))?1:input('p');
        $startIndex=($p-1)*15;
        $query['limitation']=15;
        $query['offset']=$startIndex;
        $value =  $this->client->getArrayData($countUrl,$query,3);

        if($value['code'] != 0){
            $value['data']=[];
        }
        $pager=new Page(count($totalCount['data']),15,$p);
//        dd($value);
        $page=$pager->show();
         $this->assign('page',$page);
        foreach($value['data'] as $k=>$v){
            $value['data'][$k]['createdAt']=$this->gmtToIndiaTime($v['createdAt']);
            $value['data'][$k]['updatedAt']=$this->gmtToIndiaTime($v['updatedAt']);
        }
         $this->assign('data',$value['data']);
         $this->assign('totalCount',count($totalCount['data']));
        return view();
    }

    public function userLoanInfo(){
        $id=$_GET['uid'];
        $countUrl=Uri::HOST.Uri::SELECTUSERLOAN."?id=".$id;
        $userLoanInfo=$this->client->getArrayData($countUrl);
        $userLoanInfo['data'][0]['loanSubmitTime']=date('Y-m-d H:i:s',$userLoanInfo['data'][0]['loanSubmitTime']/1000);
        $userLoanInfo['data'][0]['loanArrangedTime']=date('Y-m-d H:i:s',$userLoanInfo['data'][0]['loanArrangedTime']/1000);
        $this->assign('userLoan',$userLoanInfo['data'][0]);
        return view();
    }

}